<!--郭晓萌....2017-10-23-->
<script type="text/javascript" src="../../static/js/prompt.js" th:src="@{/js/prompt.js}"></script>
<div class="x_panel">
    <style>
        .help-block { position: absolute;
            right: -201px;
            top: 3px;
            width: 200px; }
    </style>
    <div class="x_title">
        <h2>角色
            <small> 添加</small>
        </h2>
        <div class="clearfix"></div>
    </div>
    <div class="x_content">
        <br/>

        <form id="defaultForm" method="post" class="form-horizontal  form-label-left" action="target.php">



            <div class="form-group">
                <label class="control-label col-md-3 col-sm-3 col-xs-12" for="roleName">角色名称
                </label>
                <div class="col-lg-6">
                    <input type="text" id="roleName" name="roleName" required="required"
                           oninvalid="setCustomValidity('请输入角色名称');" class="form-control col-md-7 col-xs-12"/>
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-md-3 col-sm-3 col-xs-12" for="roleCode">角色编码
                </label>
                <div class="col-lg-6">
                    <input type="text" id="roleCode" name="roleCode" required="required"
                           oninvalid="setCustomValidity('请输入角色编码');" class="form-control col-md-7 col-xs-12"/>
                </div>
            </div>
            <div class="form-group">
                <label for="middle-name" class="control-label col-md-3 col-sm-3 col-xs-12">角色描述</label>
                <div class="col-md-6 col-sm-6 col-xs-12">
                    <textarea id="middle-name" class="form-control col-md-7 col-xs-12" name="roleContent"
                              required="required" oninvalid="setCustomValidity('请输入角色描述');"></textarea>
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">

                    <button class="btn btn-primary" id="resetBtn" type="button">重置</button>
                    <button type="button" id="validateBtn" class="btn btn-primary">保存</button>
                    <button id="back" class="btn btn-success" type="button">返回</button>
                </div>
            </div>

        </form>
    </div>
</div>
<script  type="text/javascript" th:inline="javascript">
    $(function () {
        $("#back").click(function () {
            $.goTo(/*[[@{toRoleList}]]*/"./role_list.html");
        })
    })
</script>

<script type="text/javascript">
    $(document).ready(function() {
        $('form').bootstrapValidator({
//        live: 'disabled',
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },

//            submitHandler: function (form) {
//
//                $("#validateBtn").ajaxSubmit({
//                    success: function (d) {
//                        if (d == -1) {
//                            alert("系统错误，请再试或联系系统管理员！");
//                            return false;
//                        } else {
////                            if ($.isEmptyObject($("#people_id").val())) {
////                                alert("新增成功！");
////
////                            } else {
////                                alert("修改成功！");
////                            }
//                            $('#validateBtn').attr("disabled","disabled");
//                            _query(1);
//                        }
//                    }
//                });
//
//            },
        fields: {  //startTimeToDate
            roleName: {
                validators: {
                    notEmpty: {
                        message: '请输入角色名称'
                    }
                }
            },
            roleCode: {
                validators: {
                    notEmpty: {
                        message: '请输入角色编码'
                    }
                }
            },
            roleContent: {
                validators: {
                    notEmpty: {
                        message: '请输入角色描述'
                    }
                }
            }

        }

    });

        // Validate the form manually
        $('#validateBtn').click(function() {
            $('form').bootstrapValidator('validate');
            var flag = $('form').data('bootstrapValidator').isValid();

            if(flag){

                var SaveAndFlush = $("#defaultForm").serialize();
                $('#validateBtn').attr("disabled","disabled");
                $.post("/role/checkRoleUpdate", SaveAndFlush, function (result) {
                    if(result=="roleCode"){
                        $.MsgBox.Alert("提示", "角色编码重复，请重新填写");
                        $("#roleCode").val("");
                        $('#validateBtn').removeAttr("disabled","disabled")
                    }else if(result=="roleName"){
                        $.MsgBox.Alert("提示", "角色名称重复，请重新填写");
                        $("#roleName").val("");
                        $('#validateBtn').removeAttr("disabled","disabled")
                    }else {
                        $.post("/role/save", SaveAndFlush, function () {
                            $.MsgBox.Alert("提示", "存入成功");
                            $('#validateBtn').removeAttr("disabled","disabled")
                            $.goTo("toRoleList");
                        });
                    }
                });


            }else{
                return;
            }
        });

        $('#resetBtn').click(function() {
            $('form').data('bootstrapValidator').resetForm(true);

        });
//        $("form", $p).each(function() {
//            var $form = $(this);
//            $form.bootstrapValidator().on('success.form.bv', function(e) {
//                // 阻止默认事件提交
//                e.preventDefault();
//            });
//        });
    });
</script>